//
//  ComposePhotosView.m
//  SinaWebo
//
//  Created by Pink Elephant on 15/4/18.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "ComposePhotosView.h"
#import "ComposeImageView.h"
#define kMagin 10
#define kPagin 5
#define kCol 3
#define KWH ((kScreenW-(kCol-1)*kMagin- (kCol-1)*kPagin)/kCol)
#define kShowImgNum 9

@implementation ComposePhotosView
+(ComposePhotosView *)composePhotosView{
    return [[self alloc]init];
}

-(void)addImage:(UIImage *)image{
    ComposeImageView *imgView = [ComposeImageView composeImageView:image];
    [self addSubview:imgView];
//    UIButton *btn = self.subviews.lastObject;
    
//    ComposeImageView *imgView = [ComposeImageView composeImageView:image];
//    if ([btn isKindOfClass:[UIButton class]]) {
//        if (self.subviews.count<kShowImgNum) {
//            [self insertSubview:imgView atIndex:self.subviews.count-1];
//            btn.hidden = FALSE;
//            
//        }else if(self.subviews.count==kShowImgNum){
//            [self insertSubview:imgView atIndex:self.subviews.count-1];
//            btn.hidden = TRUE;
//            return;
//        } else if(self.subviews.count>kShowImgNum){
//            [MBProgressHUD showError:@"最多选择九张图片"];
//            return;
//        }
//        return;
//    }else{
//        [self addSubview:imgView];
//    }
}

-(NSArray *)getTotolImage{
    NSMutableArray *imgArray = [NSMutableArray array];
    for (UIImageView *imgView in self.subviews) {
        [imgArray addObject:imgView.image];
    }
    return imgArray;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
//    UIView *view = self.subviews.lastObject;
//    if ([view isKindOfClass:[UIImageView class]]) {
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
//        [self insertSubview:btn atIndex:self.subviews.count+1];
//    }
    
    for (int index = 0; index < self.subviews.count; index++) {
        CGFloat x = kMagin + index%kCol*(KWH+kPagin);
        CGFloat y = kMagin + index/kCol*(KWH+kPagin);
        UIImageView *imgView = self.subviews[index];
        imgView.userInteractionEnabled = YES;
        [UIView animateWithDuration:0.3 animations:^{
            imgView.frame = CGRectMake(x, y, KWH, KWH);
        }];
    }
}

-(UIImage *)getNewImage:(UIImage *)image{
    UIImage *oldImage = image;
    
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //画矩形
    CGMutablePathRef bPath = CGPathCreateMutable();
    CGRect bRect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGPathAddRect(bPath, nil, CGRectMake(0, 0, image.size.width, image.size.height));
    CGContextAddPath(ctx, bPath);
    [[UIColor redColor] set];
    CGContextFillPath(ctx);
    CGPathRelease(bPath);
    
    //画正方形
    CGMutablePathRef sPath = CGPathCreateMutable();
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.width);
    CGPathAddRect(sPath, nil, rect);
    CGContextAddPath(ctx, sPath);
    //CGContextClip(ctx);
    
    [oldImage drawInRect:bRect];
     CGPathRelease(sPath);
    
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
@end
